import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/toast_util.dart';

class ResetPasswordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ResetPasswordPageState();
  }
}

class ResetPasswordPageState extends State<ResetPasswordPage> {
  String _phone, _password, _confirmPassword, _verifyCode;
  bool _isSendVerify = false;

  TextEditingController _accountController,
      _pwdController,
      _pwd2Controller,
      _codeController;

  @override
  void initState() {
    super.initState();
    _accountController = TextEditingController();
    _pwdController = TextEditingController();
    _pwd2Controller = TextEditingController();
    _codeController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _createAppBar(),
      body: ListView(
        children: <Widget>[
          _createInputItem(false, "手机号", (str) {
            _phone = str ?? '';
          }, _accountController),
          _createInputItem(true, "密码", (str) {
            _password = str ?? '';
          }, _pwdController),
          _createInputItem(true, "确认密码", (str) {
            _confirmPassword = str ?? '';
          }, _pwd2Controller),
          _createInputItem(false, "验证码", (str) {
            _verifyCode = str ?? '';
          }, _codeController,
              child: new GestureDetector(
                child: new Text(
                  !_isSendVerify ? "发送验证码" : "验证码已发送",
                  textAlign: TextAlign.right,
                  style: new TextStyle(
                      color: !_isSendVerify
                          ? ColorCommon.primaryColor
                          : ColorCommon.grey1),
                ),
                onTap: () {
                  if (_phone != null) {
                    _getVerifyCode(_phone);
                  }
                },
              )),
        ],
      ),
    );
  }

  _createAppBar() {
    return AppBar(
      elevation: 0.0,
      title: Text("重置密码"),
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: <Widget>[
        GestureDetector(
          onTap: () {
            _resetPasswordAction();
          },
          child: Container(
            child: Text("完成"),
            alignment: Alignment.center,
            padding: const EdgeInsets.only(right: 15.0),
          ),
        ),
      ],
    );
  }

  _createInputItem(bool obscureText, String hint, Function onChanged,
      TextEditingController controller,
      {Widget child}) {
    return Container(
      height: 54.0,
      padding: const EdgeInsets.only(left: 20.0, right: 20.0),
      margin: const EdgeInsets.only(top: 20.0),
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white),
      child: Row(
        children: <Widget>[
          Expanded(
            child: TextField(
              obscureText: obscureText,
              controller: controller,
              style: TextStyle(color: ColorCommon.black1),
              decoration: InputDecoration.collapsed(
                hintText: hint,
                hintStyle: TextStyle(fontSize: 12.0, color: ColorCommon.grey1),
              ),
              onChanged: onChanged,
            ),
          ),
          child ?? Align(),
        ],
      ),
    );
  }

  _resetPasswordAction() {
    if (_phone == null || _phone.isEmpty) {
      ToastUtil.showMessageToast("请输入手机号");
      return;
    }
    if (_password == null || _password.isEmpty) {
      ToastUtil.showMessageToast("请输入密码");
      return;
    }
    if (_confirmPassword == null || _confirmPassword.isEmpty) {
      ToastUtil.showMessageToast("请输入确认密码");
      return;
    }
    if (_verifyCode == null || _verifyCode.isEmpty) {
      ToastUtil.showMessageToast("请输入验证码");
      return;
    }

    _resetPassword(_phone, _password, _confirmPassword, _verifyCode);
  }

  _resetPassword(
      String phone, String password, String confirmPassword, String code) {
    Map<String, String> params = {
      "phone": phone,
      "password": password,
      "confirm_password": confirmPassword,
      "code": code,
    };

    NetUtil.post(context, Api.userResetPassword, (data, message) {
      ToastUtil.showMessageToast(message);
      Navigator.of(context).pop();
    }, params: params, isShowLoading: true);
  }

  _getVerifyCode(String phone) {
    Map<String, String> params = {
      "phone": phone,
      "type": "password",
    };

    NetUtil.post(context, Api.identifyCodes, (data, message) {
      _isSendVerify = true;
      ToastUtil.showMessageToast(message);
    }, params: params, isShowLoading: true);
  }
}
